/*
 *    Copyright 2022 jacky4631 shen ji email:121659050@qq.com
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package com.dataoke.shoppingguide.app773026.adapter;

import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.fastjson.JSON;
import com.dataoke.shoppingguide.app773026.R;
import com.dataoke.shoppingguide.app773026.bean.dataoke.DataokeGoodsDetailBean;
import com.dataoke.shoppingguide.app773026.util.DaTaoKeUtil;
import com.dataoke.shoppingguide.app773026.util.GlideUtils;

import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author: shen ji
 * @date: 2022/3/15
 * @email: 121659050@qq.com
 * @wechat: jacky4631
 */
public class DataokeProductListAdapter extends RecyclerView.Adapter<DataokeProductListAdapter.ViewHolder> {

    private List<DataokeGoodsDetailBean> mList = new ArrayList<>();
    private Fragment fragment;

    private OnClickListener onClickListener;

    private OnItemClickListener onItemClickListener;
    public DataokeProductListAdapter(Fragment fragment) {
        this.fragment = fragment;
        initListener();
    }
    public interface OnItemClickListener {
        void onItemClick(int position, DataokeGoodsDetailBean itemBean);
    }
    /**
     * 可以共用同一个listener，相对高效
     */
    public static abstract class OnClickListener implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            RecyclerView.ViewHolder holder = (RecyclerView.ViewHolder) v.getTag();
            onClick(holder.getAdapterPosition());
        }

        public abstract void onClick(int position);
    }

    /**
     * 初始化listener
     */
    private void initListener() {
        onClickListener = new OnClickListener() {
            @Override
            public void onClick(int position) {
                if (onItemClickListener != null)
                    onItemClickListener.onItemClick(position, mList.get(position));
            }
        };

    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View inflate = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_product_list, parent, false);
        ViewHolder viewHolder = new ViewHolder(inflate);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        DataokeGoodsDetailBean contentBean = mList.get(position);

        if(position < 5) {
            holder.mDiscount.setText("券");
        } else {
            holder.mDiscount.setText("满减");
        }

        holder.title.setText(contentBean.getDtitle());

        holder.endPrice.setText(DaTaoKeUtil.getEndPrice(contentBean));
        holder.tvItemSale.setText(DaTaoKeUtil.getMonthSales(contentBean));

        List<String> specs = JSON.parseArray(contentBean.getSpecialText(), String.class);
        if(!specs.isEmpty()) {
            holder.mSpec.setText(specs.get(0));
            holder.mSpec.setVisibility(View.VISIBLE);
        }
        if(!TextUtils.isEmpty(contentBean.getBrandName())) {
            holder.mBrand.setText(contentBean.getBrandName());
            holder.mBrand.setVisibility(View.VISIBLE);
        }
        onBind(holder, position, contentBean);
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }
    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }
    public class ViewHolder extends RecyclerView.ViewHolder {

        TextView title;
        ImageView pic;
        TextView endPrice;
        ConstraintLayout layout;
        TextView tvItemSale;
        TextView mSpec;
        TextView mBrand;
        TextView mDiscount;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            title = itemView.findViewById(R.id.tv_title);
            pic = itemView.findViewById(R.id.iv_pic);
            endPrice = itemView.findViewById(R.id.tv_end_price);
            tvItemSale = itemView.findViewById(R.id.tv_item_sale);
            mSpec = itemView.findViewById(R.id.tv_spec);
            mBrand = itemView.findViewById(R.id.tv_brand);
            mDiscount = itemView.findViewById(R.id.tv_discount);
            layout = itemView.findViewById(R.id.cl_product_item);
            layout.setTag(this);
            layout.setOnClickListener(onClickListener);
        }
    }

    public void onBind(ViewHolder holder, final int position, DataokeGoodsDetailBean contentBean) {

        GlideUtils.loadImgCrop(fragment, contentBean.getMainPic(), holder.pic);


    }



    public void addData(List<DataokeGoodsDetailBean> contentBean) {
        mList.addAll(contentBean);
        notifyDataSetChanged();
    }
    public void setData(List<DataokeGoodsDetailBean> contentBean) {
        mList.clear();
        mList.addAll(contentBean);
        notifyDataSetChanged();
    }

    public List<DataokeGoodsDetailBean> getmList() {
        return mList;
    }
}
